import { NextRequest, NextResponse } from 'next/server'

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    
    // 模拟反馈数据
    const feedbacks = [
      {
        _id: 'feedback-001',
        title: '电机启动时间过长',
        content: '在测试过程中发现电机启动时间超过设计要求，需要优化启动算法。当前启动时间为2.5秒，设计要求为1.0秒以内。',
        type: 'bug' as const,
        priority: 'high' as const,
        status: 'in_progress' as const,
        createdBy: 'user-001',
        createdByName: '张测试工程师',
        createdAt: '2024-01-10T09:00:00Z',
        updatedAt: '2024-01-15T14:30:00Z',
        projectId,
        assignee: 'user-002',
        assigneeName: '李软件工程师',
        replies: [
          {
            _id: 'reply-001',
            content: '收到反馈，正在分析启动算法，预计本周内提供优化方案。',
            createdBy: 'user-002',
            createdByName: '李软件工程师',
            createdAt: '2024-01-11T10:00:00Z',
            feedbackId: 'feedback-001'
          }
        ],
        tags: ['性能', '启动时间', '算法优化']
      },
      {
        _id: 'feedback-002',
        title: '建议增加温度保护功能',
        content: '建议在电机控制器中增加温度保护功能，当电机温度过高时能够自动降功率或停机，保护电机和控制器。',
        type: 'suggestion' as const,
        priority: 'medium' as const,
        status: 'open' as const,
        createdBy: 'user-003',
        createdByName: '王产品经理',
        createdAt: '2024-01-12T14:30:00Z',
        updatedAt: '2024-01-12T14:30:00Z',
        projectId,
        rating: 4,
        tags: ['功能建议', '安全保护', '温度监控']
      },
      {
        _id: 'feedback-003',
        title: '如何配置保护参数？',
        content: '请问如何配置电机的保护参数？比如过流保护值、堵转保护时间等。在文档中没有找到详细的配置说明。',
        type: 'question' as const,
        priority: 'low' as const,
        status: 'resolved' as const,
        createdBy: 'user-004',
        createdByName: '赵客户',
        createdAt: '2024-01-08T16:00:00Z',
        updatedAt: '2024-01-09T11:00:00Z',
        projectId,
        assignee: 'user-005',
        assigneeName: '孙技术支持',
        replies: [
          {
            _id: 'reply-002',
            content: '保护参数可以通过配置文件进行设置，详细说明请参考用户手册第5章。如果还有问题，可以联系技术支持。',
            createdBy: 'user-005',
            createdByName: '孙技术支持',
            createdAt: '2024-01-09T09:00:00Z',
            feedbackId: 'feedback-003'
          }
        ],
        tags: ['配置', '保护参数', '文档']
      },
      {
        _id: 'feedback-004',
        title: '产品包装破损',
        content: '收到的产品包装有破损，虽然产品本身没有损坏，但建议改进包装设计，提高运输安全性。',
        type: 'complaint' as const,
        priority: 'medium' as const,
        status: 'closed' as const,
        createdBy: 'user-006',
        createdByName: '钱客户',
        createdAt: '2024-01-05T13:00:00Z',
        updatedAt: '2024-01-07T15:00:00Z',
        projectId,
        assignee: 'user-007',
        assigneeName: '周物流经理',
        replies: [
          {
            _id: 'reply-003',
            content: '非常抱歉给您带来不便。我们已经联系了包装供应商，正在改进包装设计。感谢您的反馈！',
            createdBy: 'user-007',
            createdByName: '周物流经理',
            createdAt: '2024-01-06T10:00:00Z',
            feedbackId: 'feedback-004'
          }
        ],
        rating: 3,
        tags: ['包装', '物流', '质量']
      },
      {
        _id: 'feedback-005',
        title: '产品质量很好',
        content: '使用了一段时间，产品质量很好，性能稳定，符合我们的预期要求。特别是保护功能很完善，使用很安心。',
        type: 'praise' as const,
        priority: 'low' as const,
        status: 'closed' as const,
        createdBy: 'user-008',
        createdByName: '吴客户',
        createdAt: '2024-01-15T18:00:00Z',
        updatedAt: '2024-01-15T18:00:00Z',
        projectId,
        rating: 5,
        tags: ['产品质量', '性能稳定', '保护功能']
      }
    ]

    return NextResponse.json(feedbacks)
  } catch (error) {
    console.error('获取项目反馈失败:', error)
    return NextResponse.json(
      { error: '获取项目反馈失败' },
      { status: 500 }
    )
  }
}

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    const body = await request.json()
    
    // 这里应该创建新反馈的逻辑
    console.log('创建新反馈:', { projectId, ...body })
    
    return NextResponse.json({ 
      message: '反馈提交成功',
      feedback: {
        _id: `feedback-${Date.now()}`,
        title: body.title,
        content: body.content,
        type: body.type,
        priority: body.priority,
        status: 'open',
        createdBy: 'current-user',
        createdByName: '当前用户',
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        projectId,
        replies: [],
        tags: body.tags || []
      }
    })
  } catch (error) {
    console.error('创建项目反馈失败:', error)
    return NextResponse.json(
      { error: '创建项目反馈失败' },
      { status: 500 }
    )
  }
}